package com.example.loginjwtdemo.controller;


import com.example.loginjwtdemo.pojo.User;
import com.example.loginjwtdemo.service.LoginService;
import com.example.loginjwtdemo.util.JwtUserHolder;
import com.example.loginjwtdemo.util.JwtUtil;
import io.jsonwebtoken.Claims;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Objects;

@RestController
@Slf4j
public class AffairController {
    @Autowired
    private LoginService loginService;
    @GetMapping("/affair1")
    public String  affair1(HttpSession session){
        User user = (User)session.getAttribute("user");
        if (Objects.nonNull(user)) {

            return "true";
        }
        return "false";
    }

    @GetMapping("/affair2")
    public String affair2(HttpSession session, HttpServletRequest request){
//        User user = RequestContext.getUser(); //通过工具类代替 session.getAttributes("user")
//        loginService.doLoginDao(user);
//        String token = request.getHeader("Authorization");
//        Claims parse = JwtUtil.parse(token);
//        if (StringUtils.isEmpty(parse)) {
//            return "权限验证失败";
//        }
        //通过ThreadLocal<T>获取user
        String currentUsername = JwtUserHolder.getCurrentUsername();
        //模拟从数据库中获取用户
        User user = new User("admin","admin");
        loginService.doLoginDao(user);
        return "操作成功！";
    }





}
